<?php

class ItemController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        session_write_close();
        $model = new MsggItem('search');
        $model->status = 999;
        $model->aid=null;
        if (isset($_GET['MsggItem'])) {
            $model->attributes = $_GET['MsggItem'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionInbox()
    {
        session_write_close();
        $model = new MsggItem('search');
        $model->status = 999;
        $model->aid=null;
        if (isset($_GET['MsggItem'])) {
            $model->attributes = $_GET['MsggItem'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        if(!Yii::app()->user->isAdmin())
        {
            $person = Yii::app()->user->getUser()->userSso->alarmPerson;
            if ($person) {
                $model->pid = $person->id;
            }
        }
        $this->render('inbox', array(
            'model' => $model,
        ));
    }


    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'info':
                $mapType = MsggItem::getOptions('type');
                $mapPerson = MsggItem::getOptions('pid');
                $mapStatus = MsggItem::getOptions('status');
                $mapAlarmType = AlarmType::getOptions('type');
                $html = '';
                $html .= '<div><b>消息ID</b> ' . $data->id . '</div>';
                $html .= '<div><b>消息时间</b> ' . (date('Y-m-d H:i:s', $data->time)) . '</div>';
                $html .= '<div><b>消息类型</b> ' . (empty($mapType[$data->type]) ? 'Type_' . $data->type : $mapType[$data->type]) . '</div>';
                $html .= '<div><b>消息状态</b> ' . (empty($mapStatus[$data->status]) ? 'Status_' . $data->status : $mapStatus[$data->status]) . '</div>';
                $html .= '<div><b>执行通道</b> ' . (empty($data->channel) ? '-' : $data->channel) . '</div>';
                $html .= '<div><b>关联人员</b> ' . (empty($mapPerson[$data->pid]) ? '-' : $mapPerson[$data->pid]) . '</div>';

                if ($data->isBundled()) {
                    $html .= '<div><b>绑定ID</b> ' . (empty($data->bid) ? '-' : $data->bid) . '</div>';
                }
                $html .= '<div><b>执行通道</b> ' . (empty($data->channel) ? '-' : $data->channel) . '</div>';
                $html .= '<div><b>重试上限</b> ' . (empty($data->retry) ? '0' : $data->retry) . '</div>';
                $html .= '<div><b>创建时间</b> ' . (empty($data->time_create) ? '-' : $data->time_create) . '</div>';
                $html .= '<div><b>更新时间</b> ' . (empty($data->time_update) ? '-' : $data->time_update) . '</div>';
                if ($data->msggApp) {
                    $html .= '<div><b>信息来源应用</b> ' .$data->msggApp->label. '</div>';
                }
                return $html;
                break;
            case 'params':
                $html = '';
                $data = json_decode($data->params, true);
                if (is_array($data) && !empty($data)) {
                    foreach ($data as $k => $v) {
                        if (is_array($v)) {
                            $v = implode(',', $v);
                        }
                        if (($vLen = strlen($v)) > 500) {
                            $v = htmlspecialchars(substr($v, 0, 500)) . '<small style="color:#999;"><i>...此处省略' . ($vLen - 500) . '字节</i></small>';
                        }
                        $html .= '<div><b>[' . ucfirst($k) . ']</b> ' . $v . '</div>';
                    }
                }
                return $html;
                break;
            case 'logs':
                $logs= $data->msgChannelLogs;
                $html = '';
                $mapStatus = MsggItem::getOptions('status');
                $mapType = MsggItem::getOptions('type');
                foreach ($logs as $log) {
                    $html .= '<div><b>[LOGID]</b> ' . $log->id . '</div>';
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[通道名称]</b> ' . $log->channel . '</div>';
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[类型]</b> ' . $mapType[$log->type] . '</div>';
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[状态]</b> ' . $mapStatus[$log->status] . '</div>';
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[创建时间]</b> ' . $log->time_create . '</div>';
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[更新时间]</b> ' . $log->time_update . '</div>';
                    $result = json_decode($log->result,true);
                    $html .= '<div style="font-size:12px;padding-left:15px;"><b>[运行结果]</b> ';
                    if(is_array($result))
                    {
                        foreach ($result as $key => $value) {
                            if(is_array($value))
                            {
                                $value=json_encode($value);
                            }
                            $html .= '<div style="font-size:8px;padding-left:15px;">' . $key . ':' . $value . '</div>';
                        }
                    }
                    $html .= '</div>';
                    //hook
                    $hooks = $log->msgItemHooks;
                    $hookParams='';
                    foreach ($hooks as $hook) {
                        $hookParams.='<div style="padding-left:15px;"><b>'.date('y-m-d H:i:s',$hook->time).'</b>:'.$hook->params.'</div>';
                    }
                    if (!empty($hookParams)) {
                        $html .= '<div style="font-size:12px;padding-left:15px;"><b>[回调]</b> ' . $hookParams . '</div>';
                    }

                    $html .= '<hr>';
                }
                return $html;
                break;
            default:
                return $data->$name;
        }
    }
}